local Msg = require("core.Msg")
local OfficialConfig = require("excel.Official")
local OfficialExcel = OfficialConfig.official
local DescExcel = OfficialConfig.desc
local RoleAttr = require("role.RoleAttr")
local Grid = require("bag.Grid")
local BagLogic = require("bag.BagLogic")
local Broadcast = require("broadcast.Broadcast")
local Lang = require("common.Lang")
local CopyCommon = require("copy.CopyCommon")
local CopyExcel = require("excel.Map").copy
local MonsterExcel = require("excel.Monster").monster
local OffcialDefine = require("official.OfficialDefine")
local ShenbingLogic = require("shenbing.ShenbingLogic")
local HorseLogic = require("horse.HorseLogic")
local BaowuLogic = require("baowu.BaowuLogic")
local EquipLogic = require("bag.EquipLogic")
local PetLogic = require("pet.PetLogic")
local ActivityDaiExcel = require("excel.Activity").dailyAct
local ItemDefine = require("bag.ItemDefine")
local ObjHuman = require("core.ObjHuman")
local Status = require("scene.Status")
--得到军衔图片
function getOfficialImage(human)
  if human.db.officialLv == 0 then
    return 0
  end
  return OfficialExcel[human.db.officialLv].image
end
--计算添加到人物身上的属性
function doCalc(human)
  if human.db.officialLv <= 0 then
    return
  end
  local addAttr = OfficialExcel[human.db.officialLv].addAttr
  for i=1, #addAttr do
    human.attr[addAttr[i][1]] = human.attr[addAttr[i][1]] + addAttr[i][2]  
  end  
end
--发送军衔列表
function sendOfficialList(human)
  local sendMsg = Msg.gc.GC_OFFICIAL_NAME_LIST
  sendMsg.list[0] = #OfficialExcel
  for i=1, #OfficialExcel do
    sendMsg.list[i].image = OfficialExcel[i].image
    sendMsg.list[i].name = OfficialExcel[i].name
    sendMsg.list[i].index = i
  end
  Msg.send(sendMsg, human.fd)
end
--检查单个是否完成
function checkOneCondition(human, str, arg)
  local len = getConditionLen(human, str)
  if len == 0 then
    return
  end
  local change = false
  local nextLv = human.db.officialLv + 1
  if str == "roleLv" then
    if OfficialExcel[nextLv][str] ~= 0 then      
        human.db.officialTarget[len - 1] = human.db.lv
        change = true
    end
  elseif str == "sbLv" then
    if next(OfficialExcel[nextLv][str]) then
      human.db.officialTarget[len - 1] = ShenbingLogic.getShenbingLvCount(human, OfficialExcel[nextLv][str][1])
      change = true
    end
  elseif str == "horseLv" then
    if OfficialExcel[nextLv][str] ~= 0 then      
      human.db.officialTarget[len - 1] = HorseLogic.getHorseLv(human)
      change = true
    end
  elseif str == "petLv" then
    if next(OfficialExcel[nextLv][str]) then
      human.db.officialTarget[len - 1] = PetLogic.getPetLvCount(human, OfficialExcel[nextLv][str][1])
      change = true
    end    
  elseif str == "baowuLv" then
    if next(OfficialExcel[nextLv][str]) then
      human.db.officialTarget[len - 1] = BaowuLogic.getBaowuLvCount(human, OfficialExcel[nextLv][str][1])
      change = true
    end
  elseif str == "qiliaoLv" then
    if OfficialExcel[nextLv][str] ~= 0 then
      human.db.officialTarget[len - 1] = human.db.QiliaoSkillLv
      change = true
    end
  elseif str == "killMonster" then
      if OfficialExcel[nextLv][str] ~= 0 then
        human.db.officialTarget[len - 1] = human.db.officialTarget[len - 1] + 1
        change = true
      end
  elseif str == "totalZhangong" then
    if OfficialExcel[nextLv][str] ~= 0 then
      human.db.officialTarget[len - 1] = human.db.zhangongHistory
      change = true      
    end
  elseif str == "enterCopy" then
    if next(OfficialExcel[nextLv][str]) then      
      for i=1, #OfficialExcel[nextLv][str] do
        if OfficialExcel[nextLv][str][i][1] == arg then
          human.db.officialTarget[len - 1] = human.db.officialTarget[len - 1] + 1
          change = true 
        end
      end
    end
  elseif str == "enterAct" then
    if next(OfficialExcel[nextLv][str]) then
      for i=1, #OfficialExcel[nextLv][str] do
        if OfficialExcel[nextLv][str][i][1] == arg then          
          human.db.officialTarget[len - 1] = human.db.officialTarget[len - 1] + 1 
          change = true
        end
      end
    end
  elseif str == "strengthLv" then
    if next(OfficialExcel[nextLv][str]) then
      human.db.officialTarget[len - 1] = EquipLogic.getStrengthLvCount(human, OfficialExcel[nextLv][str][1])
      change = true
    end      
  elseif str == "equipLv" then
    if next(OfficialExcel[nextLv][str]) then
      human.db.officialTarget[len - 1] = EquipLogic.getEquipLvCount(human, OfficialExcel[nextLv][str][1])
      change = true
    end    
  elseif str == "qualityLv" then
    if next(OfficialExcel[nextLv][str]) then
      human.db.officialTarget[len - 1] = EquipLogic.getQualityLvCount(human, OfficialExcel[nextLv][str][1])
      change = true
    end    
  elseif str == "PKTimes" then
    if OfficialExcel[nextLv][str] ~= 0 then      
      human.db.officialTarget[len - 1] = human.db.officialTarget[len - 1] + 1
      change = true
    end
  elseif str == "PKRank" then
    if OfficialExcel[nextLv][str] ~= 0 then
      human.db.officialTarget[len - 1] = human.db.offlinePkHistoryRank
      change = true      
    end
  elseif str == "bountyTask" then
    if OfficialExcel[nextLv][str] ~= 0 then
      human.db.officialTarget[len - 1] = human.db.officialTarget[len - 1] + 1
      change = true
    end  
  elseif str == "pataMax" then
    if OfficialExcel[nextLv][str] ~= 0 then
      human.db.officialTarget[len - 1] = human.db.pata_max
      change = true
    end
  elseif str == "worldBoss" then
    if OfficialExcel[nextLv][str] ~= 0 then      
      human.db.officialTarget[len - 1] = human.db.officialTarget[len - 1] + 1
      change = true
    end  
  elseif str == "familyLv" then
    if OfficialExcel[nextLv][str] ~= 0 then
      human.db.officialTarget[len - 1] = human.db.familyLv
      change = true
    end
  elseif str == "familyCopyTimes" then
    if OfficialExcel[nextLv][str] ~= 0 then
      human.db.officialTarget[len - 1] = human.db.officialTarget[len - 1] + 1
      change = true
    end
  elseif str == "taoyuanTimes" then
    if OfficialExcel[nextLv][str] ~= 0 then
      human.db.officialTarget[len - 1] = human.db.officialTarget[len - 1] + 1
      change = true
    end
  end
  if change then
    sendOfficialInfo(human)
    sendCanUpgrade(human)
  end
end
--得到条件长度
function getConditionLen(human, checkStr)
  local nextLv = human.db.officialLv + 1
  if not OfficialExcel[nextLv] then
    return false
  end
  local len = 0
  for i=1, #OffcialDefine.CONDITION do
    local str = OffcialDefine.CONDITION[i]
    if str == "enterCopy" or str == "enterAct" then
      if next(OfficialExcel[nextLv][str]) then
        for j=1, #OfficialExcel[nextLv][str] do
          len = len + 1
          if str == checkStr then
            return len
          end
        end
      end
    elseif str == "sbLv" or str == "petLv" or str == "baowuLv" or str == "strengthLv" or str == "equipLv" or str == "qualityLv" then
      if next(OfficialExcel[nextLv][str]) then
        len = len + 1
        if str == checkStr then
          return len
        end
      end
    else
      if OfficialExcel[nextLv][str] ~= 0 then
        len = len + 1
        if str == checkStr then
          return len
        end
      end
    end
  end
  return 0
end

function checkAllCondition(human)
  local nextLv = human.db.officialLv
  if not OfficialExcel[nextLv] then
    return false
  end
  local len = 0
  for i=1, #OffcialDefine.CONDITION do
    local str = OffcialDefine.CONDITION[i]
    if str == "roleLv" then
      if OfficialExcel[nextLv][str] ~= 0 then
        len = len + 1
        human.db.officialTarget[len - 1] = human.db.lv
      end
    elseif str == "sbLv" then
      if next(OfficialExcel[nextLv][str]) then
        len = len + 1
        human.db.officialTarget[len - 1] = ShenbingLogic.getShenbingLvCount(human, OfficialExcel[nextLv][str][1])
      end
    elseif str == "horseLv" then
      if OfficialExcel[nextLv][str] ~= 0 then
        len = len + 1
        human.db.officialTarget[len - 1] = HorseLogic.getHorseLv(human)
      end
    elseif str == "petLv" then
      if next(OfficialExcel[nextLv][str]) then
        len = len + 1
        human.db.officialTarget[len - 1] = PetLogic.getPetLvCount(human, OfficialExcel[nextLv][str][1])
      end    
    elseif str == "baowuLv" then
      if next(OfficialExcel[nextLv][str]) then
        len = len + 1
        human.db.officialTarget[len - 1] = BaowuLogic.getBaowuLvCount(human, OfficialExcel[nextLv][str][1])
      end
    elseif str == "qiliaoLv" then
      if OfficialExcel[nextLv][str] ~= 0 then
        len = len + 1
        human.db.officialTarget[len - 1] = human.db.QiliaoSkillLv
      end
    elseif str == "killMonster" then
    elseif str == "totalZhangong" then
      if OfficialExcel[nextLv][str] ~= 0 then
        len = len + 1
        human.db.officialTarget[len - 1] = human.db.zhangongHistory
      end
    elseif str == "enterCopy" then
    --do nothing
    elseif str == "enterAct" then
    --do nothing
    elseif str == "strengthLv" then
      if next(OfficialExcel[nextLv][str]) then
        len = len + 1
        human.db.officialTarget[len - 1] = EquipLogic.getStrengthLvCount(human, OfficialExcel[nextLv][str][1])
      end      
    elseif str == "equipLv" then
      if next(OfficialExcel[nextLv][str]) then
        len = len + 1
        human.db.officialTarget[len - 1] = EquipLogic.getEquipLvCount(human, OfficialExcel[nextLv][str][1])
      end    
    elseif str == "qualityLv" then
      if next(OfficialExcel[nextLv][str]) then
        len = len + 1
        human.db.officialTarget[len - 1] = EquipLogic.getQualityLvCount(human, OfficialExcel[nextLv][str][1])
      end    
    elseif str == "PKTimes" then
      -- do nothing
    elseif str == "PKRank" then
      if OfficialExcel[nextLv][str] ~= 0 then
        len = len + 1
        human.db.officialTarget[len - 1] = human.db.offlinePkHistoryRank
      end
    elseif str == "bountyTask" then
      --do nothing
    elseif str == "pataMax" then
    if OfficialExcel[nextLv][str] ~= 0 then
      len = len + 1
      human.db.officialTarget[len - 1] = human.db.pata_max
    end
    elseif str == "worldBoss" then
      -- do nothing
    elseif str == "familyLv" then
      if OfficialExcel[nextLv][str] ~= 0 then
        len = len + 1        
        human.db.officialTarget[len - 1] = human.db.familyLv
      end
    elseif str == "familyCopyTimes" then
     --do nothing
    elseif str == "taoyuanTimes" then
      --do nothing
    end
  end
end
function canUpgrade(human)
  local nextLv = human.db.officialLv + 1
  if not OfficialExcel[nextLv] then
    return false
  end
  local len = 0
  for i=1, #OffcialDefine.CONDITION do
    local str = OffcialDefine.CONDITION[i]
    if str == "enterCopy" or str == "enterAct" then
      if next(OfficialExcel[nextLv][str]) then
        for j=1, #OfficialExcel[nextLv][str] do
          len = len + 1
          if human.db.officialTarget[len - 1] < OfficialExcel[nextLv][str][j][2] then
            return false
          end 
        end
      end
    elseif str == "sbLv" or str == "petLv" or str == "baowuLv" or str == "strengthLv" or str == "equipLv" or str == "qualityLv" then
      if next(OfficialExcel[nextLv][str]) then
        len = len + 1
        if human.db.officialTarget[len - 1] < OfficialExcel[nextLv][str][2] then
          return false
        end
      end
    else
      if OfficialExcel[nextLv][str] ~= 0 then
        len = len + 1
        if human.db.officialTarget[len - 1] < OfficialExcel[nextLv][str] then
          return false
        end
      end
    end
  end
  return true
end
--发送军衔系统信息
function sendOfficialInfo(human)
  local sendMsg = Msg.gc.GC_OFFICIAL_INFO
  local nextLv = human.db.officialLv + 1
  sendMsg.curLv = human.db.officialLv
  local cache = {}
  sendMsg.addAttr[0] = 0

  sendMsg.rewardInfo = human.db.officialRewardIsGet
  if OfficialExcel[nextLv] then
    RoleAttr.addAttrs(sendMsg.addAttr, OfficialExcel[nextLv].addAttr)
    sendMsg.condition[0] = 0
    for i=1, #OffcialDefine.CONDITION do
      setDesc(human, sendMsg.condition, OffcialDefine.CONDITION[i])      
    end
    sendMsg.yinliang = OfficialExcel[nextLv].yinliang
    sendMsg.zhangong = OfficialExcel[nextLv].zhangong
    sendMsg.exp = OfficialExcel[nextLv].exp
  else
    sendMsg.yinliang = 0
    sendMsg.zhangong = 0
    sendMsg.exp = 0
    sendMsg.condition[0] = 0
  end
  if human.db.officialLv == 0 then
    sendMsg.addAttr[0] = 0
    sendMsg.yinliang = 0
    sendMsg.zhangong = 0
    sendMsg.exp = 0
    sendMsg.rewardInfo = 0
  end

  Msg.send(sendMsg, human.fd)
end
function getReward(human)
  if human.db.officialRewardIsGet ~= 1 or human.db.officialLv == 0 then
    return
  end
  local curLv = human.db.officialLv
  
  ObjHuman.addZhangong(human, OfficialExcel[curLv].zhangong, "official")
  ObjHuman.addYinliang(human, OfficialExcel[curLv].yinliang, "official")
  ObjHuman.addExp(human, OfficialExcel[curLv].exp)
  
  Broadcast.sendBroadcast(human, Lang.DOWN, Lang.OFFICIAL_GET_REWARD_SUCCESS)  
  human.db.officialRewardIsGet = 2
  sendOfficialInfo(human)
end

function sendCanUpgrade(human)
  local sendMsg = Msg.gc.GC_OFFICIAL_CAN_UPGRADE
  sendMsg.canUpgrade = canUpgrade(human) and 1 or 0  
  Msg.send(sendMsg, human.fd)
end

function upgrade(human) 
  sendOfficialInfo(human)
  local curLv = human.db.officialLv
  if not OfficialExcel[curLv + 1] then
    return
  end
  if not canUpgrade(human) then
    return Broadcast.sendBroadcast(human, Lang.DOWN, Lang.OFFICIAL_UPGRADE_CONDITION_NOT_ENOUGH)
  end
  human.db.officialLv = human.db.officialLv + 1
  for i=0, 25 - 1 do
    human.db.officialTarget[i] = 0
  end  
  checkAllCondition(human)
  sendOfficialInfo(human)
  Status.sendViewStatus(human, true)
  Broadcast.sendBroadcast(human, Lang.DOWN, Lang.OFFICIAL_UPGRADE_SUCCESS)
end

function setDesc(human, info, str)
  local nextLv = human.db.officialLv + 1
  if str == "enterCopy" or str == "enterAct" then
    if next(OfficialExcel[nextLv][str]) then
      for i=1, #OfficialExcel[nextLv][str] do
        info[0] = info[0] + 1
        info[info[0]].desc = getDesc(human, str, OfficialExcel[nextLv][str][i][1])
        info[info[0]].completed = human.db.officialTarget[info[0] - 1]
        info[info[0]].needCon = OfficialExcel[nextLv][str][i][2]
      end
    end
  elseif str == "sbLv" or str == "petLv" or str == "baowuLv" or str == "strengthLv" or str == "equipLv" or str == "qualityLv" then
    if next(OfficialExcel[nextLv][str]) then
      info[0] = info[0] + 1
      info[info[0]].desc = getDesc(human, str, OfficialExcel[nextLv][str][1])      
      info[info[0]].completed = human.db.officialTarget[info[0] - 1]
      info[info[0]].needCon = OfficialExcel[nextLv][str][2]
    end
  else
    if OfficialExcel[nextLv][str] ~= 0 then
      info[0] = info[0] + 1      
      info[info[0]].desc = getDesc(human, str, OfficialExcel[nextLv][str])
      info[info[0]].completed = human.db.officialTarget[info[0] - 1]
      info[info[0]].needCon = OfficialExcel[nextLv][str]
    end
  end
end

function getDesc(human, str, arg)
  if str == "enterCopy" then
    if CopyExcel[arg].difficult ~= -1 then
      return string.format(DescExcel[str].desc, CopyExcel[arg].name .. "-" .. CopyCommon.getCopyDifficult(CopyExcel[arg].difficult).."难度")
    else
      return string.format(DescExcel[str].desc, CopyExcel[arg].name)
    end
  elseif str == "enterAct" then
    for kk,vv in pairs(ActivityDaiExcel) do
      if vv.copyId == arg then
        return string.format(DescExcel[str].desc, vv.name)
      end
    end    
  elseif str == "PKTimes" or str == "bountyTask" or str == "worldBoss" or str == "familyCopyTimes" or str == "taoyuanTimes" then
    return DescExcel[str].desc
  elseif str == "qualityLv" then
    return string.format(DescExcel[str].desc, ItemDefine.getText(arg))
  else
    return string.format(DescExcel[str].desc, arg)
  end
end